import requests
import re
import scrapy
from scrapy.pipelines.files import FilesPipeline
from scrapy.utils.project import get_project_settings
import os

class GetshipvideoPipeline(FilesPipeline):

    def get_media_requests(self, item, info):
        pass

    def file_path(self, request, response=None, info=None, *, item=None):
        pattern = re.compile(r'[^\u4e00-\u9fa5]')
        text = str(request.meta['desc'])
        text = re.sub(pattern, '', text)
        filename = text + ".mp4"
        return filename

    def item_completed(self, results, item, info):
        return item

    def download_video(self, url, filename):
        if os.path.exists(filename):
            print(f"文件 {filename} 已存在，跳过下载。")
            return

        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Language": "en-US,en;q=0.5",
            "Connection": "keep-alive",
            "Referer": "https://www.vcg.com"
        }

        response = requests.get(url, headers=headers, stream=True)
        if response.status_code == 200:
            with open(filename, 'wb') as file:
                for chunk in response.iter_content(chunk_size=1024):
                    if chunk:
                        file.write(chunk)
            print(f"视频下载完成: {filename}")
        else:
            print(f"视频下载失败: {response.status_code}")